package com.xingkong.web.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xingkong.common.annotation.Log;
import com.xingkong.common.core.controller.BaseController;
import com.xingkong.common.core.domain.AjaxResult;
import com.xingkong.common.core.page.TableDataInfo;
import com.xingkong.common.core.text.Convert;
import com.xingkong.common.enums.BusinessType;
import com.xingkong.common.utils.poi.ExcelUtil;
import com.xingkong.product.domain.ProductPlate;
import com.xingkong.product.service.ProductPlateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 商品板块，标识商品是否在某些板块中显示Controller
 *
 * @author yiyue
 * @date 2020-03-15
 */
@Controller
@RequestMapping("/product/plate")
public class ProductPlateController extends BaseController {
    private String prefix = "product/plate";

    @Autowired
    private ProductPlateService productPlateService;

    @RequiresPermissions("product:plate:view")
    @GetMapping()
    public String plate() {
        return prefix + "/plate";
    }

    /**
     * 查询商品板块，标识商品是否在某些板块中显示列表
     */
    @RequiresPermissions("product:plate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductPlate productPlate) {
        startPage();
        return getDataTable(productPlateService.list(new QueryWrapper<ProductPlate>(productPlate)));
    }

    /**
     * 导出商品板块，标识商品是否在某些板块中显示列表
     */
    @RequiresPermissions("product:plate:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductPlate productPlate) {
        List<ProductPlate> list = productPlateService.list(new QueryWrapper<>());
        ExcelUtil<ProductPlate> util = new ExcelUtil<ProductPlate>(ProductPlate.class);
        return util.exportExcel(list, "plate");
    }

    /**
     * 新增商品板块，标识商品是否在某些板块中显示
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品板块，标识商品是否在某些板块中显示
     */
    @RequiresPermissions("product:plate:add")
    @Log(title = "商品板块，标识商品是否在某些板块中显示", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductPlate productPlate) {
        return toAjax(productPlateService.save(productPlate));
    }

    /**
     * 修改商品板块，标识商品是否在某些板块中显示
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProductPlate productPlate = productPlateService.getById(id);
        mmap.put("productPlate", productPlate);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品板块，标识商品是否在某些板块中显示
     */
    @RequiresPermissions("product:plate:edit")
    @Log(title = "商品板块，标识商品是否在某些板块中显示", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductPlate productPlate) {
        return toAjax(productPlateService.updateById(productPlate));
    }

    /**
     * 删除商品板块，标识商品是否在某些板块中显示
     */
    @RequiresPermissions("product:plate:remove")
    @Log(title = "商品板块，标识商品是否在某些板块中显示", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(productPlateService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
